﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SmartMathLibrary
{
    /// <summary>
    /// This class represents a complex point in a three dimensional space.
    /// </summary>
    [Serializable]
    public class ComplexPoint3D : IComplexPoint3D
    {
        /// <summary>
        /// The x value of the complex point.
        /// </summary>
        private ComplexNumber x;

        /// <summary>
        /// The y value of the complex point.
        /// </summary>
        private ComplexNumber y;

        /// <summary>
        /// The z value of the complex point.
        /// </summary>
        private ComplexNumber z;

        /// <summary>
        /// Initializes a new instance of the <see cref="ComplexPoint3D"/> class.
        /// </summary>
        public ComplexPoint3D()
        {
            this.x = new ComplexNumber();
            this.y = new ComplexNumber();
            this.z = new ComplexNumber();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ComplexPoint3D"/> class.
        /// </summary>
        /// <param name="x">The x value of the complex point.</param>
        /// <param name="y">The y value of the complex point.</param>
        /// <param name="z">The z value of the complex point.</param>
        public ComplexPoint3D(ComplexNumber x, ComplexNumber y, ComplexNumber z)
        {
            if (x == (ComplexNumber) null)
            {
                throw new ArgumentNullException("x");
            }

            if (y == (ComplexNumber) null)
            {
                throw new ArgumentNullException("y");
            }

            if (z == (ComplexNumber) null)
            {
                throw new ArgumentNullException("z");
            }

            this.x = x.Copy();
            this.y = y.Copy();
            this.z = z.Copy();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ComplexPoint2D"/> class.
        /// </summary>
        /// <param name="point">The complex point to clone.</param>
        public ComplexPoint3D(ComplexPoint3D point)
        {
            if (point == (ComplexPoint3D) null)
            {
                throw new ArgumentNullException("point");
            }

            this.x = point.X.Copy();
            this.y = point.Y.Copy();
            this.Z = point.Z.Copy();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ComplexPoint3D"/> class.
        /// </summary>
        /// <param name="point">The point which describes the real number part of the complex point.</param>
        public ComplexPoint3D(Point3D point)
        {
            if (point == (Point3D) null)
            {
                throw new ArgumentNullException("point");
            }

            this.x = new ComplexNumber(point.X);
            this.y = new ComplexNumber(point.Y);
            this.Z = new ComplexNumber(point.Z);
        }

        /// <summary>
        /// Gets or sets the x value of the complex point.
        /// </summary>
        /// <value>The x value of the complex point.</value>
        public ComplexNumber X
        {
            get { return x; }
            set { x = value; }
        }

        /// <summary>
        /// Gets or sets the y value of the complex point.
        /// </summary>
        /// <value>The y value of the complex point.</value>
        public ComplexNumber Y
        {
            get { return y; }
            set { y = value; }
        }

        /// <summary>
        /// Gets or sets the z value of the complex point.
        /// </summary>
        /// <value>The z value of the complex point.</value>
        public ComplexNumber Z
        {
            get { return z; }
            set { z = value; }
        }

        /// <summary>
        /// Adds a x, y and z value to the current point instance.
        /// </summary>
        /// <param name="xValue">The x value.</param>
        /// <param name="yValue">The y value.</param>
        /// <param name="zValue">The y value.</param>
        public void Add(double xValue, double yValue, double zValue)
        {
            this.x += xValue;
            this.y += yValue;
            this.z += zValue;
        }

        /// <summary>
        /// Adds a x, y and z value to the current point instance.
        /// </summary>
        /// <param name="xValue">The x value.</param>
        /// <param name="yValue">The y value.</param>
        /// <param name="zValue">The y value.</param>
        public void Add(ComplexNumber xValue, ComplexNumber yValue, ComplexNumber zValue)
        {
            if (xValue == (ComplexNumber) null)
            {
                throw new ArgumentException("xValue == (ComplexNumber)null != false", "xValue");
            }

            if (yValue == (ComplexNumber) null)
            {
                throw new ArgumentException("yValue == (ComplexNumber)null != false", "yValue");
            }

            if (zValue == (ComplexNumber) null)
            {
                throw new ArgumentException("zValue == (ComplexNumber)null != false", "zValue");
            }

            this.x += xValue;
            this.y += yValue;
            this.z += zValue;
        }

        /// <summary>
        /// Copies the current instance of the complex point.
        /// </summary>
        /// <returns>A copy of the current complex point instance.</returns>
        public ComplexPoint3D Copy()
        {
            return new ComplexPoint3D(this.x.Copy(), this.y.Copy(), this.z.Copy());
        }

        /// <summary>
        /// Scales the position elements x, y and z with a specified factor.
        /// </summary>
        /// <param name="scaleFactor">The scale factor.</param>
        public void ScaleTransformation(double scaleFactor)
        {
            this.x *= scaleFactor;
            this.y *= scaleFactor;
            this.z *= scaleFactor;
        }

        /// <summary>
        /// Scales the position elements x, y and z with a specified factor.
        /// </summary>
        /// <param name="scaleFactor">The scale factor.</param>
        public void ScaleTransformation(ComplexNumber scaleFactor)
        {
            if (scaleFactor == (ComplexNumber) null)
            {
                throw new ArgumentException("complex reason", "scaleFactor");
            }

            this.x *= scaleFactor;
            this.y *= scaleFactor;
            this.z *= scaleFactor;
        }

        /// <summary>
        /// Scales the position elements x, y and z with a specified factor.
        /// </summary>
        /// <param name="scaleFactorX">The scale factor for the x position.</param>
        /// <param name="scaleFactorY">The scale factor for the y position.</param>
        /// <param name="scaleFactorZ">The scale factor for the z position.</param>
        public void ScaleTransformation(double scaleFactorX, double scaleFactorY, double scaleFactorZ)
        {
            this.x *= scaleFactorX;
            this.y *= scaleFactorY;
            this.z *= scaleFactorZ;
        }

        /// <summary>
        /// Scales the position elements x, y and z with a specified factor.
        /// </summary>
        /// <param name="scaleFactorX">The scale factor for the x position.</param>
        /// <param name="scaleFactorY">The scale factor for the y position.</param>
        /// <param name="scaleFactorZ">The scale factor for the z position.</param>
        public void ScaleTransformation(ComplexNumber scaleFactorX, ComplexNumber scaleFactorY,
                                        ComplexNumber scaleFactorZ)
        {
            if (scaleFactorX == (ComplexNumber) null)
            {
                throw new ArgumentException("complex reason", "scaleFactorX");
            }

            if (scaleFactorY == (ComplexNumber) null)
            {
                throw new ArgumentException("complex reason", "scaleFactorY");
            }

            if (scaleFactorZ == (ComplexNumber) null)
            {
                throw new ArgumentException("complex reason", "scaleFactorZ");
            }

            this.x *= scaleFactorX;
            this.y *= scaleFactorY;
            this.z *= scaleFactorZ;
        }

        /// <summary>
        /// Translates the point position.
        /// </summary>
        /// <param name="translateValue">The translate value for the x-, y- and z position.</param>
        public void TranslateTransformation(double translateValue)
        {
            this.x += translateValue;
            this.y += translateValue;
            this.z += translateValue;
        }

        /// <summary>
        /// Translates the point position.
        /// </summary>
        /// <param name="translateValue">The translate value for the x-, y- and z position.</param>
        public void TranslateTransformation(ComplexNumber translateValue)
        {
            if (translateValue == (ComplexNumber) null)
            {
                throw new ArgumentException("complex reason", "translateValue");
            }

            this.x += translateValue;
            this.y += translateValue;
            this.z += translateValue;
        }

        /// <summary>
        /// Translates the point position.
        /// </summary>
        /// <param name="translateValueX">The translate value for x position.</param>
        /// <param name="translateValueY">The translate value for y position.</param>
        /// <param name="translateValueZ">The translate value for z position.</param>
        public void TranslateTransformation(double translateValueX, double translateValueY, double translateValueZ)
        {
            this.x += translateValueX;
            this.y += translateValueY;
            this.z += translateValueZ;
        }

        /// <summary>
        /// Translates the point position.
        /// </summary>
        /// <param name="translateValueX">The translate value for x position.</param>
        /// <param name="translateValueY">The translate value for y position.</param>
        /// <param name="translateValueZ">The translate value for z position.</param>
        public void TranslateTransformation(ComplexNumber translateValueX, ComplexNumber translateValueY,
                                            ComplexNumber translateValueZ)
        {
            if (translateValueX == (ComplexNumber) null)
            {
                throw new ArgumentException("complex reason", "translateValueX");
            }

            if (translateValueY == (ComplexNumber) null)
            {
                throw new ArgumentException("complex reason", "translateValueY");
            }

            if (translateValueZ == (ComplexNumber) null)
            {
                throw new ArgumentException("complex reason", "translateValueZ");
            }

            this.x += translateValueX;
            this.y += translateValueY;
            this.z += translateValueZ;
        }

        /// <summary>
        /// Implements the operator ==.
        /// </summary>
        /// <param name="a">The first complex point.</param>
        /// <param name="b">The second complex point.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator ==(ComplexPoint3D a, ComplexPoint3D b)
        {
            if (System.Object.ReferenceEquals(a, b))
            {
                return true;
            }

            if (((object) a == null) || ((object) b == null))
            {
                return false;
            }

            return ((a.X == b.X) && (a.Y == b.Y));
        }

        /// <summary>
        /// Implements the operator !=.
        /// </summary>
        /// <param name="a">The first complex point.</param>
        /// <param name="b">The second complex point.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator !=(ComplexPoint3D a, ComplexPoint3D b)
        {
            return !(a == b);
        }

        /// <summary>
        /// Implements the operator +.
        /// </summary>
        /// <param name="a">The first complex point.</param>
        /// <param name="b">The second complex point.</param>
        /// <returns>The result of the operator.</returns>
        public static ComplexPoint3D operator +(ComplexPoint3D a, ComplexPoint3D b)
        {
            if (a == (ComplexPoint3D) null)
            {
                throw new ArgumentNullException("a");
            }

            if (b == (ComplexPoint3D) null)
            {
                throw new ArgumentNullException("b");
            }

            ComplexPoint3D result = new ComplexPoint3D();

            result.X = a.X + b.X;
            result.Y = a.Y + b.Y;
            result.Z = a.Z + b.Z;

            return result;
        }

        /// <summary>
        /// Implements the operator -.
        /// </summary>
        /// <param name="a">The first complex point.</param>
        /// <param name="b">The second complex point.</param>
        /// <returns>The result of the operator.</returns>
        public static ComplexPoint3D operator -(ComplexPoint3D a, ComplexPoint3D b)
        {
            if (a == (ComplexPoint3D) null)
            {
                throw new ArgumentNullException("a");
            }

            if (b == (ComplexPoint3D) null)
            {
                throw new ArgumentNullException("b");
            }

            ComplexPoint3D result = new ComplexPoint3D();

            result.X = a.X - b.X;
            result.Y = a.Y - b.Y;
            result.Z = a.Z - b.Z;

            return result;
        }

        /// <summary>
        /// Implements the operator *.
        /// </summary>
        /// <param name="a">The first complex point.</param>
        /// <param name="b">The second complex point.</param>
        /// <returns>The result of the operator.</returns>
        public static ComplexPoint3D operator *(ComplexPoint3D a, ComplexPoint3D b)
        {
            if (a == (ComplexPoint3D) null)
            {
                throw new ArgumentNullException("a");
            }

            if (b == (ComplexPoint3D) null)
            {
                throw new ArgumentNullException("b");
            }

            ComplexPoint3D result = new ComplexPoint3D();

            result.X = a.X * b.X;
            result.Y = a.Y * b.Y;
            result.Z = a.Z * b.Z;

            return result;
        }

        /// <summary>
        /// Implements the operator /.
        /// </summary>
        /// <param name="a">The first complex point.</param>
        /// <param name="b">The second complex point.</param>
        /// <returns>The result of the operator.</returns>
        public static ComplexPoint3D operator /(ComplexPoint3D a, ComplexPoint3D b)
        {
            if (a == (ComplexPoint3D) null)
            {
                throw new ArgumentNullException("a");
            }

            if (b == (ComplexPoint3D) null)
            {
                throw new ArgumentNullException("b");
            }

            ComplexPoint3D result = new ComplexPoint3D();

            result.X = a.X / b.X;
            result.Y = a.Y / b.Y;
            result.Z = a.Z / b.Z;

            return result;
        }

        /// <summary>
        /// Converts the current point to a complex vector.
        /// </summary>
        /// <returns>The converted complex vector.</returns>
        public GeneralComplexVector ToVector()
        {
            GeneralComplexVector result = new GeneralComplexVector(3);

            result.SetComponent(0, this.x);
            result.SetComponent(1, this.y);
            result.SetComponent(2, this.z);

            return result;
        }

        /// <summary>
        /// Converts the current point to a complex matrix.
        /// </summary>
        /// <returns>The converted complex matrix.</returns>
        public ComplexMatrix ToMatrix()
        {
            return this.ToVector().ToMatrix();
        }

        /// <summary>
        /// Determines whether the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <param name="obj">The <see cref="T:System.Object"/> to compare with the current <see cref="T:System.Object"/>.</param>
        /// <returns>
        /// true if the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>; otherwise, false.
        /// </returns>
        /// <exception cref="T:System.NullReferenceException">The <paramref name="obj"/> parameter is null.</exception>
        public override bool Equals(object obj)
        {
            return base.Equals(obj);
        }

        /// <summary>
        /// Serves as a hash function for a particular type.
        /// </summary>
        /// <returns>
        /// A hash code for the current <see cref="T:System.Object"/>.
        /// </returns>
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        /// <summary>
        /// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </returns>
        public override string ToString()
        {
            return this.x.ToString() + "; " + this.y.ToString() + "; " + this.z.ToString();
        }
    }
}